short int valA0;
short int tempx;
unsigned char ledLight=0;
const int analogOutPin=6;   //LED light control

void setup() {
  analogWrite(analogOutPin, ledLight);
  Serial.begin(9600);
}

void loop() {
  valA0 = analogRead(A0);
  
  if(valA0 >= 620 && valA0 <= 650){
    tempx = convert(valA0, 650, 620, -20, 0); 
  }
  else if(valA0 >= 550 && valA0 <= 620){
    tempx = convert(valA0, 620, 550, 0, 20);
  } 
  else if(valA0 >= 450 && valA0 <= 550){
    tempx = convert(valA0, 550, 450, 20, 40);
  }
  else if(valA0 >= 340 && valA0 <= 450){
    tempx = convert(valA0, 450, 340, 40, 60);
  } 
  else if(valA0 >= 220 && valA0 <= 340){
    tempx = convert(valA0, 340, 220, 60, 80);
  }
  else if(valA0 >= 160 && valA0 <= 220){
    tempx = convert(valA0, 220, 160, 80, 100);
  }
    Serial.println( tempx ); 
    Serial.println( valA0 );
    ledLight = -2 * tempx + 200; 
    analogWrite(analogOutPin, ledLight);
}

short int convert(
   short int ax,
   short int a0,short int a1,
   short int temp0,short int temp1)
{
   short int tempx;
   tempx = temp0+(ax-a0)*(temp1-temp0)/(a1-a0);
   return tempx;
}

